package design

/**
 * 静态代理模式 2020年4月13日 12:22:32
 */
fun main() {
    val company = WeddingCompany(Person())
    company.happyMarry()
    println("------")
    val company2 = WeddingCompany2(Person())
    company2.happyMarry()
}

/*
人生四大喜事:
    久旱逢甘露
    他乡遇故知
    洞房花烛夜
    金榜提名时
人生三大谎言：
    老年人说：活够啦
    生意人说：不赚钱呐
    三十多的大姑娘说：不想嫁人呀
 */
interface Marry {
    fun happyMarry()
}

//真实角色
class Person : Marry {
    override fun happyMarry() {
        println("某某人结婚了")
    }
}

//代理角色
class WeddingCompany(private val target: Marry) : Marry {
    override fun happyMarry() {
        beforeMarry()
        target.happyMarry()
        afterMarry()
    }

    private fun beforeMarry() {
        println("举行婚礼")
    }

    private fun afterMarry() {
        println("收付尾款")
    }
}

//class WeddingCompany2(target: Marry) : Marry by target {
//class WeddingCompany2() : Marry by Person() {
class WeddingCompany2(private val target: Marry) : Marry by target {
    override fun happyMarry() {
        beforeMarry()
        target.happyMarry()
        afterMarry()
    }

    private fun beforeMarry() {
        println("举行婚礼")
    }

    private fun afterMarry() {
        println("收付尾款")
    }
}